﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InTheHand.Net.Sockets;

namespace BlueAd
{
    public partial class frmNewCampain : Form
    {
        public frmNewCampain(List<BluetoothDeviceInfo> devices)
        {
            InitializeComponent();
            Campain = new Campain("New campain");
            Campain.Id = Guid.NewGuid();
            Campain.TargetDevices = devices;
            InitUi();
        }

        public frmNewCampain(List<BluetoothDeviceInfo> devices, Campain campain)
        {
            InitializeComponent();
            Campain = campain;
            InitUi();
        }

        public Campain Campain { get; set; }

        private void InitUi()
        {
            txtName.Text = Campain.Name;
            txtDescription.Text = Campain.Description;
            StartTime.Value = Campain.Start;
            EndTime.Value = Campain.End;
            checkTargetAllDevices.Checked = Campain.TargetAllDevices;
            foreach (BluetoothDeviceInfo targetDevice in Campain.TargetDevices)
            {
                lvTargetDevices.Items.Add(targetDevice.DeviceName);
            }
            foreach (FileInfo fileInfo in Campain.Files)
            {
                lvFiles.Items.Add(fileInfo.FullName);
            }
        }

        private void UpdateDuration()
        {
            TimeSpan duration = new TimeSpan(0, 0, 0, 0);
            if (StartTime != null && EndTime != null)
            {
                duration = EndTime.Value - StartTime.Value;
            }
            Campain.Duration = duration;
            RefreshControls();
        }

        public void RefreshControls()
        {
            lblDuration.Text = Campain.Duration.ToString();
        }

        private void StartTime_ValueChanged(object sender, EventArgs e)
        {
            UpdateDuration();
            StartHour.Text = (sender as DateTimePicker).Value.ToString("t");
        }

        private void EndTime_ValueChanged(object sender, EventArgs e)
        {
            UpdateDuration();
            EndHour.Text = (sender as DateTimePicker).Value.ToString("t");
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Campain.Name = txtName.Text;
            Campain.Description = txtDescription.Text;
            Campain.Start = StartTime.Value;
            Campain.End = EndTime.Value;
            Campain.Duration = EndTime.Value - StartTime.Value;
            Campain.TargetAllDevices = checkTargetAllDevices.Checked;

            if (Campain != null)
                Exit(true);
            else
                Exit(false);
            
        }

        private void Exit(bool ok)
        {
            if(ok)
            {
                this.DialogResult = DialogResult.OK;
                Close();   
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void btnAddFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            if (openFileDialog1.CheckFileExists)
            {
                lvFiles.Items.Add(openFileDialog1.FileName);
                Campain.Files.Add(new FileInfo(openFileDialog1.FileName));
            }
        }

        private void StartHour_Validated(object sender, EventArgs e)
        {
            string a = StartTime.Value.ToString("d");
            string b = (sender as MaskedTextBox).Text;
            DateTime c = DateTime.Parse(a + " "+  b);
        }

        private void EndHour_Validated(object sender, EventArgs e)
        {
            string a = EndTime.Value.ToString("d");
            string b = (sender as MaskedTextBox).Text;
            DateTime c = DateTime.Parse(a + " " + b);
        }
    }
}
